﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Netcached.Tests
{
    [TestClass]
    public class TestMultiGet : BaseTestClass
    {
        [TestMethod]
        public void TestGetMultipleValues()
        {
            string key = "TestMultiGet.TestGetMultipleValues";
            int numberOfValues = 10;

            Dictionary<string, int> values = new Dictionary<string, int>();
            // generate values
            Random rnd = new Random();
            for (int i = 0; i < numberOfValues; i++)
            {
                int value = rnd.Next();
                values.Add(key + i, value);
            }

            // set values
            foreach (var item in values)
            {
                Client.Set(item.Key, item.Value);
            }

            var fetchedValues = Client.Get(values.Keys.ToArray());

            bool foundAllValues = true;
            string message = null;

            foreach (var item in values)
            {
                var fetchedItem = fetchedValues.FirstOrDefault(x => x.Key == item.Key);
                if (fetchedItem == null)
                {
                    message = string.Format("Couldn't find an item with key: {0}", item.Key);
                    foundAllValues = false;
                    break;
                }
                else if ((int)fetchedItem.Item != item.Value)
                {
                    message = string.Format("Item with key '{0}' has different values: {1} : {2}", item.Key, item.Value, fetchedItem.Item.ToString() ?? "null");
                    foundAllValues = false;
                    break;
                }
            }

            Assert.IsTrue(foundAllValues, message);
        }
    }
}
